package com.shxc.smarttrade.old.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.adapter.MoneyRecordAdapter;
import com.shxc.smarttrade.old.bean.PaymentInfo;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.myview.MyFootView;
import com.shxc.smarttrade.old.myview.MyRefreshHeadView;
import com.shxc.smarttrade.old.myview.RecycleViewDivider;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;
import com.shxc.smarttrade.old.utils.SpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/6/7.
 */

public class AccountInfomationActivity extends BaseActivity {


    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.rv_record)
    XRecyclerView xRecyclerView;

    private LinearLayoutManager manager;
    private MoneyRecordAdapter adapter;
    private int page = 1;
    private List<PaymentInfo.DataBean.PaymentBean> paymentBeanListAdd = new ArrayList<>();
    private int type = 0;//1: 充值 2：提现

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what = 0) {
                case 0:
                    PaymentInfo paymentInfo = (PaymentInfo) msg.obj;
                    if (paymentInfo.getErr_code().equals("0")) {
                        List<PaymentInfo.DataBean.PaymentBean> paymentBeanList = paymentInfo.getData().getPayment();
                        if (paymentBeanList.size() > 0) {
                            paymentBeanListAdd.addAll(paymentBeanList);
                            adapter.setDatas(paymentBeanListAdd);
                            xRecyclerView.loadMoreComplete();
                        } else {
                            if (xRecyclerView != null)
                                xRecyclerView.setNoMore(true);
                            showToast("已加载全部！");
                        }
                    } else {
                        showToast(paymentInfo.getErr_msg());
                    }
                    if (xRecyclerView != null)
                        xRecyclerView.refreshComplete();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_infomation);
        ButterKnife.bind(this);
        initView();
        showShortDialog();
        initData(SpUtils.getInt(this, "userId"), 1, 10);
        initListener();
    }

    private void initView() {
        type = getIntent().getIntExtra("type", 0);
        if (type == 1)
            tvCenter.setText("充值记录");
        else
            tvCenter.setText("提现记录");

        manager = new LinearLayoutManager(this);
        xRecyclerView.setLayoutManager(manager);
        xRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayout.VERTICAL, 1, getResources().getColor(R.color.dividing_line_color)));
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
        MyRefreshHeadView myRefreshHeadView = new MyRefreshHeadView(this);
        myRefreshHeadView.setArrowImageView(R.mipmap.indicator_arrow);
        xRecyclerView.setRefreshHeader(myRefreshHeadView);
        xRecyclerView.setFootView(new MyFootView(this));
        xRecyclerView.setAdapter(adapter = new MoneyRecordAdapter(this));
    }

    /**
     * 初始化监听器
     */
    private void initListener() {
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        paymentBeanListAdd.clear();
                        page = 1;
                        initData(SpUtils.getInt(getApplicationContext(), "userId"), page, 10);
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        initData(SpUtils.getInt(getApplicationContext(), "userId"), page, 10);
//                        xRecyclerView.loadMoreComplete();
                    }
                }, 1000);
            }
        });
    }

    @OnClick(R.id.rl_left)
    public void onRlLeftClicked() {
        finish();
    }

    /**
     * 初始化数据
     */
    private void initData(int userId, int page, int pagesize) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", SpUtils.getString(this, "accessToken", ""));
        map.put("type", String.valueOf(type));
        map.put("userId", userId + "");
        map.put("page", page + "");
        map.put("pagesize", pagesize + "");
        MyOkHttpUtils.getServiceDatas(this,map, URLContents.ACCOUNT_INFOMATION_ACTIVITY_PAYMENTIONFO, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void success(String response, int id) {
                if (response != null) {
                    Gson gson = new Gson();
                    PaymentInfo paymentInfo = gson.fromJson(response, PaymentInfo.class);
                    Message msg = Message.obtain();
                    msg.what = 0;
                    msg.obj = paymentInfo;
                    handler.sendMessage(msg);
                }
                dismissShortDialog();
            }

            @Override
            public void faild(Call call, Exception e, int id) {
                showToast(R.string.net_error);
                dismissShortDialog();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
